use zmath::{Vec2};

use zert::glutils::*;
use zert::graphics;
use zert::Camera2D;

fn main() {
    use glfw::{ Action, Context, Key, WindowMode };

    let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS)
        .expect("Failed to init glfw");

    let (mut window, events) = glfw.create_window(800, 600, "Draw Sprite", WindowMode::Windowed)
        .unwrap();

    window.make_current();

    zert::load(|s| window.get_proc_address(s));

    window.set_all_polling(true);

    let texture = {
        let img = image::open("images/dummy.png").unwrap().into_rgb();
        let (width, height) = img.dimensions();

        graphics::Texture::with_pixels(width, height, graphics::TextureFormat::RGB, &img.into_raw())
    };
    
    let mut camera = Camera2D::new();

    while !window.should_close() {
        glfw.poll_events();
        for (_, event) in glfw::flush_messages(&events) {
            match event {
                glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
                    window.set_should_close(true)
                }
                glfw::WindowEvent::CursorPos(x, y) => {
                    camera.set_position(Vec2(-400.0 + x as f32, 300.0 - y as f32));
                }
                _ => {},
            }
        }

        gl_clear_color(1.0, 1.0, 1.0, 1.0);
        gl_clear(gl::COLOR_BUFFER_BIT);

        window.swap_buffers();
    }
}